/**
 * 作者:李杰
 * email: maillijie@gmail.com
 */
package com.javainfo.util;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Date;
import java.util.HashSet;
import java.util.Set;
import java.util.UUID;

import org.apache.commons.io.FilenameUtils;
import org.apache.struts2.ServletActionContext;

import com.javainfo.entity.Article;
import com.javainfo.entity.Attachments;
import com.javainfo.entity.User;

public class UploadFileManageUtil {

	public static void saveUploadFileToDB(FileUploadSupport obj, User user, Article article) {
		File[] srcFiles = obj.getUploadFiles();
		Set<Attachments> attachmentsSet = new HashSet<Attachments>();
		// 处理每个要上传的文件
		Set<Article> articleSet = new HashSet<Article>();
		articleSet.add(article);
		if (srcFiles != null) {
			for (int i = 0; i < srcFiles.length; i++) {
				Attachments attachments = new Attachments();
				attachments.setFileName(obj.getUploadFilesFileName()[i]);
				attachments.setFileType(obj.getUploadFilesContentType()[i]);
				Date uploadTime = new Date();
				attachments.setUploadTime(uploadTime);
				attachments.setUpdateTime(uploadTime);
				attachments.setHasSaveToDB(true);
				attachments.setIsSystemFile(false);
				attachments.setUploadUser(user);
				attachments.setContent(UploadFileManageUtil
						.getFileByte(srcFiles[i]));
				attachmentsSet.add(attachments);
				attachments.setArticleSet(articleSet);
			}
		}
		article.setAttachmentsSet(attachmentsSet);
	}

	public static void copyUploadFiles(FileUploadSupport obj,User user, Article article) {
		File[] srcFiles = obj.getUploadFiles();
		Set<Attachments> attachmentsSet = new HashSet<Attachments>();
		// 处理每个要上传的文件
		String userName = user.getUserAccount();
		Set<Article> articleSet = new HashSet<Article>();
		articleSet.add(article);
		if (srcFiles != null) {
			for (int i = 0; i < srcFiles.length; i++) {
				String theFileName = UploadFileManageUtil
						.getRandomFileName(obj.getUploadFilesFileName()[i]);
				// 根据服务器的文件保存地址和原文件名创建目录文件全路径
				String dstPath = ServletActionContext.getServletContext()
						.getRealPath(obj.getSavePath())
						+ Const.FILE_PATH_SPLIT
						+ userName
						+ Const.FILE_PATH_SPLIT + theFileName;
				File dstFile = new File(dstPath);
				UploadFileManageUtil.copy(srcFiles[i], dstFile);
				Attachments attachments = new Attachments();
				attachments.setFileName(obj.getUploadFilesFileName()[i]);
				attachments.setFileType(obj.getUploadFilesContentType()[i]);
				Date uploadTime = new Date();
				attachments.setUploadTime(uploadTime);
				attachments.setUpdateTime(uploadTime);
				attachments.setUploadedPath(obj.getSavePath()
						+ Const.FILE_PATH_SPLIT + userName
						+ Const.FILE_PATH_SPLIT + theFileName);
				attachments.setHasSaveToDB(false);
				attachments.setIsSystemFile(false);
				attachments.setUploadUser(user);
				attachmentsSet.add(attachments);
				attachments.setArticleSet(articleSet);
			}
		}
		article.setAttachmentsSet(attachmentsSet);
	}
	
	// 复制文件
	public static void copy(File src, File dst) {
		InputStream in = null;
		OutputStream out = null;
		dst.getParentFile().mkdirs();
		try {
			in = new BufferedInputStream(new FileInputStream(src),
					Const.UploadFileManage.BUFFER_SIZE);
			out = new BufferedOutputStream(new FileOutputStream(dst, true),
					Const.UploadFileManage.BUFFER_SIZE);
			byte[] buffer = new byte[Const.UploadFileManage.BUFFER_SIZE];
			int len = 0;
			while ((len = in.read(buffer)) > 0) {
				out.write(buffer, 0, len);
			}
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			if (null != in) {
				try {
					in.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
			if (null != out) {
				try {
					out.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
	}
	
	public static String getRandomFileName(String fileName) {
		String extension = FilenameUtils.getExtension(fileName);
		return UUID.randomUUID().toString().replace(Const.HYPHEN,
				Const.EMPTY_STRING)
				+ Const.POINT + extension;
	}

	public static byte[] getFileByte(File file) {
		InputStream in = null;
		byte[] buffer = null;
		try {
			FileInputStream fi = new FileInputStream(file);
			in = new BufferedInputStream(fi, fi.available());
			buffer = new byte[fi.available()];
			in.read(buffer);
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return buffer;
	}
}
